package com.open.capacity.prints.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.prints.dao.BillGhyhzzsqDao;
import com.open.capacity.prints.entity.BillGhyhzzsq;
import com.open.capacity.prints.service.BillGhyhzzsqService;
import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class BillGhyhzzsqServiceImpl implements BillGhyhzzsqService {

    @Autowired
    private BillGhyhzzsqDao billGhyhzzsqDao;

    /**
     * 添加
     *
     * @param billGhyhzzsq
     */
    public String save(BillGhyhzzsq billGhyhzzsq) {
        billGhyhzzsq.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billGhyhzzsq.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        if(billGhyhzzsq.getDjrq() != "") {
            calendar.setTime(DateUtil.parseDate2(billGhyhzzsq.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
            billGhyhzzsq.setYear(calendar.get(Calendar.YEAR));                    //获取年份
            billGhyhzzsq.setMonth(String.valueOf(calendar.get(Calendar.MONTH) + 1));                    //获取月份
            billGhyhzzsq.setDay(String.valueOf(calendar.get(Calendar.DATE)));            //获取日
            billGhyhzzsq.setDjrq(billGhyhzzsq.getDjrq().replaceAll("-", "").replaceAll(" ", ""));
        }
        if(billGhyhzzsq.getMoney2() != null) {
            billGhyhzzsq.setMoney2(billGhyhzzsq.getMoney2().divide(new BigDecimal(100)));
            billGhyhzzsq.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhyhzzsq.getMoney2()));
        }
        billGhyhzzsqDao.save(billGhyhzzsq);
        return billGhyhzzsq.getId();
    }

    /**
     * 修改
     *
     * @param billGhyhzzsq
     */
    public String update(BillGhyhzzsq billGhyhzzsq) {
        billGhyhzzsq.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        if(billGhyhzzsq.getDjrq() != "") {
            calendar.setTime(DateUtil.parseDate2(billGhyhzzsq.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
            billGhyhzzsq.setYear(calendar.get(Calendar.YEAR));                    //获取年份
            billGhyhzzsq.setMonth(String.valueOf(calendar.get(Calendar.MONTH) + 1));                    //获取月份
            billGhyhzzsq.setDay(String.valueOf(calendar.get(Calendar.DATE)));            //获取日
            billGhyhzzsq.setDjrq(billGhyhzzsq.getDjrq().replaceAll("-", "").replaceAll(" ", ""));
        }
        if(billGhyhzzsq.getMoney2() != null) {
            billGhyhzzsq.setMoney2(billGhyhzzsq.getMoney2().divide(new BigDecimal(100)));
            billGhyhzzsq.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhyhzzsq.getMoney2()));
        }
        billGhyhzzsqDao.update(billGhyhzzsq);
        return billGhyhzzsq.getId();
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billGhyhzzsqDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillGhyhzzsq findById(String id) {
        BillGhyhzzsq item = billGhyhzzsqDao.findById(id);
        BigDecimal resut = item.getMoney2();
        if(resut != null) {
            Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
            item.setMillion5(million5Count);
            resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

            Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
            item.setMillion4(million4Count);
            resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


            Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
            item.setMillion3(million3Count);
            resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

            Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
            item.setMillion2(million2Count);
            resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

            Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
            item.setMillion1(million1Count);
            resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

            Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
            item.setThousand(thousandCount);
            resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

            Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundred(hundredCount);
            resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

            Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
            item.setTen(tenCount);
            resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

            Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
            item.setYuan(yuanCount);
            resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

            Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
            item.setJiao(jiaoCount);
            resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

            Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
            item.setFen(fenCount);
        }
        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillGhyhzzsq> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillGhyhzzsq> list = billGhyhzzsqDao.findAll(params);
        PageInfo<BillGhyhzzsq> pageInfo = new PageInfo(list);

        return PageResult.<BillGhyhzzsq>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillGhyhzzsq> findAll(Map<String, Object> params) {
        return billGhyhzzsqDao.findAll(params);
    }

    /**
     * 查询数据
     *
     * @param wtdwFullname
     * @return
     */
    public List<BillGhyhzzsq> findByWtdwFullname (String wtdwFullname){
        return billGhyhzzsqDao.findByWtdwFullname(wtdwFullname);
    }
}
